package com.lagou.edu.config;

import com.lagou.edu.annotations.A;
import com.lagou.edu.application.AnnotationBeanApplication;
import com.lagou.edu.factory.AnnotationSummerBeanFactory;
import com.lagou.edu.factory.TransactionProxyFactory;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class CustomServletContextListener implements ServletContextListener {

    private static String SCAN_PATH = "com.lagou.edu";

    @A
    AnnotationSummerBeanFactory annotationSummerBeanFactory;

    @A
    TransactionProxyFactory transactionProxyFactory;

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        AnnotationBeanApplication annotationBeanApplication = new AnnotationBeanApplication();
        AnnotationSummerBeanFactory annotationSummerBeanFactory = AnnotationSummerBeanFactory.init(annotationBeanApplication);
        annotationSummerBeanFactory.doScan(SCAN_PATH);
        try {
            annotationSummerBeanFactory.doImports();
            annotationSummerBeanFactory.doAutoWrite();
            annotationSummerBeanFactory.doTransaction();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
